package mspbots.xero.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

/**
 * mspbots.data.cw.utils.QueryBody
 * <a href="https://developer.connectwise.com/Products/Manage/Developer_Guide">developer.connectwise.com</a>
 * =, !=, <, <=, >, >=, contains, like, not
 * <p>
 * Strings	Must be surrounded by quotes	Summary = "This is my string" (Accepts *'s for Wild Cards)
 * Integers	No formatting required	Board/Id = 123
 * Boolean	No formatting required but must be True or False	ClosedFlag = True
 * Datetimes	Must be surrounded by square brackets	LastUpdated = [2016-08-20T18:04:26Z]
 * Operators	<, <=, =, !=, >, >=, contains, like, in, not	Summary Not Contains "Low Priority"
 * Logic Operators	Supported operators include:
 * AND
 * OR
 * board/name="integration" and summary="xyz"
 * board/name="integration" or board/name="professional services"
 * <p>
 * Reference*	Must have a / followed by the field under the reference you would like to use	manufacturer/name
 *
 * @author <a href="https://github.com/vnobo">Alex bob</a>
 * @date Created by 2020/7/9
 */
@Data
@ApiModel("Connect wise api query model")
public class QueryBody {

    @ApiModelProperty(value = "Search results based on the fields returned in a GET. default 500", example = "board/name=\"Integration\"\n" +
            "summary=\"xyz\"\n" +
            "board/id in (3, 2, 4)\n" +
            "lastUpdated > [2016-08-20T18:04:26Z]\n" +
            "Only fields returned in a GET request can be used")
    private String conditions;

    @ApiModelProperty(value = "Allows searching arrays on endpoints that list childConditions under parameters",
            example = "/company/contacts?childconditions=communicationItems/value like \"john@Outlook.com\" AND communicationItems/communicationType=\"Email\"")
    private String childConditions;

    @ApiModelProperty(value = "Allows searching custom fields when customFieldConditions is listed in the parameters",
            example = "/company/contacts?customFieldConditions=caption=\"TomNumber\" AND value !=null")
    private String customFieldConditions;

    @ApiModelProperty(value = "Choose which field to sort the results by,asc or desc", example = "contact/name asc")
    private String orderBy;


    @ApiModelProperty(value = "Limits which information is returned in the response",
            example = "company/companies?fields=id,name,status/id")
    private String fields;

    @ApiModelProperty(value = "Limits which information is returned in the response",
            example = "system/reports/service?columns=id,summary,name")
    private String columns;

    @Builder
    public QueryBody(String conditions, String childConditions, String customFieldConditions, String orderBy, String fields, String columns) {
        this.conditions = conditions;
        this.childConditions = childConditions;
        this.customFieldConditions = customFieldConditions;
        this.orderBy = orderBy;
        this.fields = fields;
        this.columns = columns;
    }

    /**
     * default query params
     * max records 25, id>0
     *
     * @return default query body
     */
    public static QueryBody withDefault() {
        return QueryBody.builder().build();
    }

    /**
     * query params with FilterExpression
     * max records 500
     *
     * @param expression fiter expression
     * @return default query body
     */
    public static QueryBody withConditions(String expression) {
        QueryBody queryBody = withDefault();
        queryBody.setConditions(expression);
        return queryBody;
    }
}
